<template>
  <div class="app-container">
    <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="关键词">
        <el-input clearable v-model="searchForm.keyword" placeholder="搜索"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <selected-status clearable @change="setStatus"
                         :options="[{label: '全部',value:''},{label:'待使用',value:0},{label:'已使用',value:1},{label:'已过期',value:2}]"
        ></selected-status>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button title="添加" type="success" @click="$router.push({name:'couponForm'})">添加</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="#" width="95" prop="coupon_id"/>

      <el-table-column label="类型" width="130">
        <template v-slot:default="{scope,row}">
          <el-tag :type="row.coupon_type==10?'success':'primary'">{{ row.coupon_type_desc }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="领取人" width="130"  >
        <template v-slot:default="{scope,row}">
          <el-tag type="info" @click="$router.push({ path: '/user/index',query:{user_id:row.user_id} })">
            {{row.user_info.username}}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="名称" width="150" prop="name"/>
      <el-table-column label="状态" width="100" align="center">
        <template v-slot:default="{scope,row}">
          <el-tag v-if="row.status==0" type="success">{{ row.status_desc }}</el-tag>
          <el-tag v-if="row.status==1" type="primary">{{ row.status_desc }}</el-tag>
          <el-tag v-if="row.status==2" type="info">{{ row.status_desc }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="满减金额" width="100" align="center" prop="reduce_price"/>
      <el-table-column label="折扣" width="100" align="center" prop="discount"/>
      <el-table-column label="有效期类型" width="130">
        <template v-slot:default="{scope,row}">
          <el-tag :type="row.expire_type==10?'success':'primary'">{{ row.expire_type_desc }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" width="180" align="center" prop="startTime"/>
      <el-table-column label="结束时间" width="180" align="center" prop="endTime"/>

      <el-table-column label="领取时间" width="180" align="center" prop="created_at"/>

    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                @pagination="fetchData"
    />
  </div>

</template>

<script>
import api from '@/api/coupon'
import Pagination from '@/components/Pagination'
import selectedStatus from '@/components/tools/selectedStatus.vue'

export default {
  components: {
    Pagination, selectedStatus
  },
  filters: {},
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
        sort: ''
      },
      searchForm: {
        keyword: ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    setStatus(value) {
      this.searchForm.status = value
      this.search()
    },
    fetchData() {
      this.listLoading = true
      api.coupon_log(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    search() {
      this.listQuery.page = 1
      this.listQuery = Object.assign(this.listQuery, this.searchForm)
      this.fetchData()
    }
  }

}
</script>

<style scoped>

</style>
